package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.service.DtsGoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsGoodsProductServiceImpl implements DtsGoodsProductService {

    @Autowired
    DtsGoodsProductMapper goodsProductMapper;
    @Override
    public Integer getTotal() {

        Integer count = goodsProductMapper.selectCount(null);

        return count;
    }

    /**
     * 1. 根据商品id删除 库存表数据
     * 2. 添加新的库存数据
     * @param goodsId
     * @param products
     */
    @Override
    public void updateProducts(Integer goodsId, DtsGoodsProduct[] products) {
        //删除该商品对应的库存记录
        //创建删除条件
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId, goodsId);
        goodsProductMapper.delete(qw);


        //添加多个库存对象
        if(products != null && products.length > 0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(goodsId);
                goodsProductMapper.insert(product);
            }
        }

    }


    @Override
    public void deleteByGoodsId(Integer goodId) {
        //条件是商品id，  修改的是状态
        //要修改的数据
        DtsGoodsProduct product = new DtsGoodsProduct();
        product.setDeleted(true);

        //条件
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId, goodId);


        goodsProductMapper.update(product, qw);


    }

    @Override
    public void addProduct(Integer goodsId, DtsGoodsProduct[] products) {
        //添加多个库存对象
        if(products != null && products.length > 0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(goodsId);
                goodsProductMapper.insert(product);
            }
        }
    }
}
